//@ sourceURL=/static/modules/management/management.js
let searchUrl = '/ext/queryDmpFileExtList';
let addUrl = '/ext/addDmpFileExt';
let deleteUrl = '/ext/delDmpFileExt';
let fileUrl = '/ext/queryDmpFileMap';
let findAllUrl = '/dataSource/findAll';
let mapFileUrl = '/mapper/selectMapperXml';
// let mapNameList = [];
var managementView = new View({
    name: 'management',
    init_page() {
        console.log('management-load');
        $('.selectpickerToday').selectpicker($.po('selectpicker', {
            container: 'body',
        }))
        $('#management_add_container [data-plugin="selectpicker"]').selectpicker($.po('selectpicker', {
            container: 'body',
            width:'260px'
        }))  
        $('#datetimepicker').datetimepicker({
            format: 'yyyy-mm-dd hh:ii',
            language: 'zh-CN',
            weekStart: 1,
            autoclose: 1,
            startDate: new Date()
        }).on('hide',function(e) {  
            managementView.plugins.management_add_form.formValidation.revalidateField('singleTime');
        }); 
        $('#datetimeStart').datetimepicker({
            language: 'zh-CN',
            format: 'yyyy-mm-dd',
            weekStart: 1,
            /*以星期一为一星期开始*/
            todayBtn: true,
            autoclose: 1,
            minView: 2,
            startDate: new Date(),
            pickerPosition:'top-right'
            /*精确到天*/
        }).on("changeDate", function (ev) { //值改变事件
            //选择的日期不能大于第二个日期控件的日期
            if (ev.date) {
                $("#datetimeEnd").datetimepicker('setStartDate', new Date(ev.date.valueOf()));
            } else {
                $("#datetimeEnd").datetimepicker('setStartDate', null);
            }
        }).on('hide',function(e) {  
            managementView.plugins.management_add_form.formValidation.revalidateField('validStartDate');
        }); 
        $('#datetimeEnd').datetimepicker({
            language: 'zh-CN',
            format: 'yyyy-mm-dd',
            weekStart: 1,
            /*以星期一为一星期开始*/
            todayBtn: 1,
            autoclose: 1,
            minView: 2,
            pickerPosition:'top-right'
            // startDate: new Date()
            /*精确到天*/
        }).on("changeDate", function (ev) {
            //选择的日期不能小于第一个日期控件的日期
            if (ev.date) {
                $("#datetimeStart").datetimepicker('setEndDate', new Date(ev.date.valueOf()));
            } else {
                $("#datetimeStart").datetimepicker('setEndDate', new Date());
            }
        }).on('hide',function(e) {  
            managementView.plugins.management_add_form.formValidation.revalidateField('validEndDate');
        }); 
    }


});
/**
 * 方法
 */
managementView.fn = {
    manageSubmit(){
        if(!managementView.plugins.transferBox.changeData()[0]){
            alertify.error('请选择映射文件');
            return;
        }
        if($('input:radio[name="repeatFlag"]:checked').val() !=0 && $('input:radio[name="repeatFlag"]:checked').val() !=1){
            alertify.error('请选择执行频率');
            return;
        }
        
        managementView.plugins.management_add_form.$form.submit()
    },
    add() {
        managementView.plugins.management_add_container.show();
        managementView.plugins.transferBox.init_data();
        $('.selectpickerToday').attr('disabled', 'disabled');
        $('.form-group-loopTime .btn-group>.btn:first-child').addClass('disabled');
        $('.selectpickerToday').selectpicker('val', '');
        $('.starttime').val('');
        $('.endtime').val('');
        $('.singleTime').val('');
        wdAjax.ajax({
            type: 'get',
            url: findAllUrl,
            success: function (data) {
                $('.selectpicker').find('option').remove();
                $('.selectpicker').append('<option class="selectOpt" value="">请选择</option>');
                data.forEach(element => {
                    let dom = '<option class="selectOpt" value="' + element.id + '">' + element.dsName + '</option>';
                    $('.selectpicker').append(dom);
                });
                $('.selectpicker').selectpicker('refresh');
            }
        })
    },
    choose() {
        var val = $('input:radio[name="repeatFlag"]:checked').val();
        if (val == 0) {
            $('.singleTime').attr('disabled', false);
            $('.selectpickerToday').attr('disabled', 'disabled');
            $('.form-group-loopTime .btn-group>.btn:first-child').addClass('disabled');
            $('.starttime').attr('disabled', true);
            $('.endtime').attr('disabled', true);
            $('.selectpickerToday').selectpicker('val', '');
            $('.starttime').val('');
            $('.endtime').val('');
        } else if (val == 1) {
            $('.singleTime').attr('disabled', true);
            $('.selectpickerToday').removeAttr('disabled');
            $('.form-group-loopTime .btn-group>.btn:first-child').removeClass('disabled');
            $('.starttime').attr('disabled', false);
            $('.endtime').attr('disabled', false);
            $('.singleTime').val('');
        }
    },
    details(e) {
        let ename = $(e).data('ename');
        let jobName = $(e).data('name');
        if($(e).data('cntnum')==0){
            alertify.error('该条任务暂无记录');
            return;
        }
        router.to('missions', {
            type: ename,
            itemId: jobName
        })
    },
    flies(e) {
        let id = $(e).data('id');
        $('.title_message').empty();
        $('.content_message_list').empty();
        wdAjax.post({
            url: fileUrl,
            cache: false, //禁用缓存
            data: {
                id: id
            },
            success: function (data) {
                $('.title_message').append(data.jobName);
                var html;
                data.mapFileNames.forEach((item, index) => {
                    html = '<li>' + item + '</li>';
                    $('.content_message_list').append(html);
                })
                managementView.plugins.files_details.show();
            }
        })
    },
    delete(e) {
        var id = $(e).data('id');
        swal($.po('sweetalert', {
            title: "确定要删除该任务吗？",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: '#DD6B55',
            confirmButtonText: '删除',
            cancelButtonText: "取消",
            closeOnConfirm: false,
        }), function (isConfirm) {
            if (isConfirm) {
                wdAjax.post({
                    url: deleteUrl,
                    cache: false, //禁用缓存
                    data: {
                        id: id
                    },
                    success: function (data) {
                        alertify.success('删除成功');
                        swal.close();
                        managementView.plugins.management_list.api.draw();
                    }
                })
            }
        });
    },
    render: {
        operation(data, type) {
            var html = '<button class="btn btn-primary margin-right-10 grid-delete-btn" data-name="' + data.jobName + '" data-id="' + data.id + '" type="button" onClick="managementView.fn.delete(this)" >删除</button>' + '<button class="btn btn-primary margin-right-10 grid-edit-btn" data-ename="' + data.ename + '" data-name="' + data.jobName + '" data-id="' + data.id + '" type="button"  data-cntnum ="'+ data.logCntNum + '"  onClick="managementView.fn.details(this)">记录</button>'
            return html;
        },
        files(data, type) {
            var html = '<button class="btn btn-primary margin-right-10 grid-delete-btn" data-name="' + data.mapFileNum + '" data-id="' + data.id + '" type="button" onClick="managementView.fn.flies(this)" >' + data.mapFileNum + '</button>'
            return html;
        }

    }
}
/**
 * 事件
 */
managementView._event = () => {

}
/**
 * 绑定插件
 */
managementView.use(new Dialog({
    name: 'management_add_container',
    container_selector: '#management_add_container',
    width: '860px',
    title: "添加任务",
    init_callback: function () {

    }
}))
managementView.use(new Dialog({
    name: 'files_details',
    container_selector: '#files_details',
    width: '700px',
    height: '500px',
    title: "包含文件数",
    init_callback: function () {

    }
}))
managementView.use(new Table({
    name: 'management_list',
    selector: '#management_list',
    url: searchUrl,
    search_btn: "#search_btn",
    callback: function (data) {
        
    }
}))
managementView.use(new Form({
    name: 'management_add_form',
    selector: '#management_add_form',
    url: addUrl,
    before_callback: function (data) {
        data['dsFromName'] = $('.dsFromId').find('option:selected').text();
        data['dsToName'] = $('.dsToId').find('option:selected').text();
        data['mapNameList'] = managementView.plugins.transferBox.changeData();
        if(data['singleTime']){
            let singleTime = data['singleTime'] + ":00";
            data['singleTime'] = singleTime;
        }
        return data;
    },
    success: function () {
        alertify.success('新增成功');
        managementView.plugins.management_add_container.close();
        managementView.plugins.management_list.api.draw();
    },
    callback: function () {

    }
}))
managementView.use(new Transfer({
    name: 'transferBox',
    selector: '#transferBox',
    title:'选择映射文件：',
    url: mapFileUrl,
    callback: function () {

    }
}))


